﻿package com.tudou.skin.common.button
{
    import com.tudou.skin.common.events.*;
    import flash.display.*;
    import flash.events.*;

    public class BaseButton extends Sprite implements IButton
    {
        protected var rollOverOutSprite:Sprite;
        protected var keyArr:Array;
        protected var extendClick:Boolean = false;
        protected var buttonArr:Array;
        protected var index:int = -1;
        protected var iOverOut:IOverOut;

        public function BaseButton()
        {
            buttonArr = [];
            keyArr = [];
            rollOverOutSprite = new Sprite();
            addChild(rollOverOutSprite);
            return;
        }// end function

        public function get activeKey() : String
        {
            return keyArr[index];
        }// end function

        public function set clickContainer(param1:Boolean) : void
        {
            var _loc_2:InteractiveObject = null;
            if (param1 == extendClick)
            {
                return;
            }
            extendClick = param1;
            var _loc_4:* = extendClick;
            this.buttonMode = extendClick;
            this.useHandCursor = _loc_4;
            if (extendClick)
            {
                this.addEventListener(MouseEvent.CLICK, buttonClickHlr);
            }
            else
            {
                this.removeEventListener(MouseEvent.CLICK, buttonClickHlr);
            }
            var _loc_3:int = 0;
            while (_loc_3 < buttonArr.length)
            {
                
                _loc_2 = buttonArr[_loc_3] as InteractiveObject;
                if (!extendClick)
                {
                    _loc_2.addEventListener(MouseEvent.CLICK, buttonClickHlr);
                }
                else
                {
                    _loc_2.removeEventListener(MouseEvent.CLICK, buttonClickHlr);
                }
                _loc_3++;
            }
            return;
        }// end function

        public function addStateButton(param1:InteractiveObject, param2:Number = 0, param3:Number = 0, param4:String = null) : void
        {
            param1.x = param2;
            param1.y = param3;
            param1.visible = false;
            addChild(param1);
            param1.addEventListener(MouseEvent.CLICK, buttonClickHlr);
            buttonArr.push(param1);
            if (param4)
            {
                keyArr.push(param4);
            }
            else
            {
                keyArr.push(param1.name);
            }
            return;
        }// end function

        public function getSkin() : InteractiveObject
        {
            return this;
        }// end function

        private function buttonClickHlr(event:MouseEvent) : void
        {
            dispatchEvent(new ButtonEvent(ButtonEvent.CLICK, false, false, index, activeKey));
            return;
        }// end function

        public function get clickContainer() : Boolean
        {
            return extendClick;
        }// end function

        private function rollOutHlr(event:MouseEvent) : void
        {
            iOverOut.isOver = false;
            return;
        }// end function

        public function set activeIndex(param1:int) : void
        {
            var _loc_2:InteractiveObject = null;
            var _loc_3:int = 0;
            while (_loc_3 < buttonArr.length)
            {
                
                _loc_2 = buttonArr[_loc_3] as InteractiveObject;
                _loc_2.visible = false;
                _loc_3++;
            }
            if (param1 >= 0 && param1 < buttonArr.length)
            {
                index = param1;
                _loc_2 = buttonArr[index] as InteractiveObject;
                _loc_2.visible = true;
            }
            else
            {
                index = -1;
            }
            return;
        }// end function

        public function setBackground(param1:DisplayObject, param2:Number = 0, param3:Number = 0) : void
        {
            param1.x = param2;
            param1.y = param3;
            addChildAt(param1, 0);
            return;
        }// end function

        public function set activeKey(param1:String) : void
        {
            var _loc_2:* = keyArr.indexOf(param1);
            activeIndex = _loc_2;
            return;
        }// end function

        public function get activeIndex() : int
        {
            return index;
        }// end function

        public function get buttonNum() : uint
        {
            return buttonArr.length;
        }// end function

        public function removeRollOverOut() : void
        {
            if (iOverOut)
            {
                rollOverOutSprite.removeChild(iOverOut.getDisplay());
                this.removeEventListener(MouseEvent.ROLL_OVER, rollOverHlr);
                this.removeEventListener(MouseEvent.ROLL_OUT, rollOutHlr);
                iOverOut = null;
            }
            return;
        }// end function

        public function addRollOverOut(param1:IOverOut) : void
        {
            if (param1 == null)
            {
                return;
            }
            removeRollOverOut();
            iOverOut = param1;
            rollOverOutSprite.addChild(iOverOut.getDisplay());
            this.addEventListener(MouseEvent.ROLL_OVER, rollOverHlr);
            this.addEventListener(MouseEvent.ROLL_OUT, rollOutHlr);
            return;
        }// end function

        private function rollOverHlr(event:MouseEvent) : void
        {
            iOverOut.isOver = true;
            return;
        }// end function

    }
}
